package com.ergwang.service.admin;

import com.ergwang.Model.*;
import com.ergwang.Model.admin.bo.GoodsBO;
import com.ergwang.Model.admin.bo.GoodsTypeBO;
import com.ergwang.Model.admin.bo.MassageReplyBO;
import com.ergwang.Model.admin.bo.SpecBO;
import com.ergwang.Model.admin.vo.GoodsInfoVO;
import com.ergwang.Model.mall.vo.SearchGoodsVO;
import com.ergwang.dao.GoodsDao;
import com.ergwang.dao.GoodsDaoImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：ergwang
 * @Date: 2020/5/30
 * @Time: 15:17
 */
public class GoodsServiceImpl implements GoodsService {

    GoodsDao goodsDao = new GoodsDaoImpl();

    public List<Type> getType() {
        return goodsDao.getType();
    }

    public List<Goods> getGoodsByType(String typeId) {
        return goodsDao.getGoodsByType(typeId);
    }

    public int deleteGoods(String id) {
        return goodsDao.deleteGoods(id);
    }

    /**
     * 判断是否已经存在了，存在了就拒绝添加
     *
     * @param goodsTypeBO
     * @return
     */
    public int addType(GoodsTypeBO goodsTypeBO) {
        List<Type> typeList = goodsDao.getType();
        if (typeList.contains(goodsTypeBO.getName())) {
            return 0;
        }
        return goodsDao.addType(goodsTypeBO);
    }

    public GoodsInfoVO getGoodsInfo(GoodsInfoVO goodsInfoVO) {
        // 获取goodsInfoVO 中的specs
        goodsInfoVO.setSpecs(goodsDao.getGoodsInfo_specs(goodsInfoVO));
        // 获取goodsInfoVO 中的goods
        goodsInfoVO.setGoods(goodsDao.getGoodsInfo_goods(goodsInfoVO));

        return goodsInfoVO;
    }

    /**
     * 新增商品的price ，stockNum 需要用specList转换得到
     * 1。保存数据到商品表
     * 2。拿到刚刚插入的商品的id
     * 3。
     *
     * @param goodsBO
     */
    public void addGoods(GoodsBO goodsBO) {
        List<SpecBO> specList = goodsBO.getSpecList();
        Double unitPrice = specList.get(0).getUnitPrice();
        Integer stockNum = specList.get(0).getStockNum();
        for (int i = 1; i < specList.size(); i++) {
            if (unitPrice > specList.get(i).getUnitPrice()) {
                unitPrice = specList.get(i).getUnitPrice();
            }
            if (stockNum < specList.get(i).getStockNum()) {
                stockNum = specList.get(i).getStockNum();
            }
        }

        Goods goods = new Goods(null,
                goodsBO.getImg(),
                goodsBO.getName(),
                unitPrice,
                goodsBO.getTypeId(),
                stockNum,
                goodsBO.getDesc());
        goodsDao.addGoods(goods);

        // 获取新增商品的id
        int lastInsertId = goodsDao.lastInsertId(goods.getName());
        List<Spec> specs = new ArrayList<Spec>();
        for (SpecBO specBO : specList) {
            Spec spec = new Spec(null,
                    specBO.getSpecName(),
                    specBO.getStockNum(),
                    specBO.getUnitPrice(),
                    lastInsertId);
            specs.add(spec);
        }
        goodsDao.addSpecs(specs);
    }

    /**
     * 添加规格到数据库，再返回刚刚添加的规格数据
     * @param addSpec
     * @return
     */
    public Spec addSpec(Spec addSpec) {
        // 添加
        List<Spec> specs = new ArrayList<Spec>();
        specs.add(addSpec);
        goodsDao.addSpecs(specs);

        // 获取刚刚添加的然后返回
        return goodsDao.getSpecs(addSpec.getGoodsId());
    }

    /**
     * 根据specName 和 goodsId 删除对应的spec
     * @param deleteSpec
     */
    public void deleteSpec(Spec deleteSpec) {
        goodsDao.deleteSpec(deleteSpec);
    }

    /**
     * 根据id将goods对象更新到数据库中
     * @param goods
     */
    public void updateGoods(Goods goods) {
        goodsDao.updateGoods(goods);
    }

    public void deleteType(String typeId) {
        goodsDao.deleteType(typeId);
    }

    public void msg_reply(MassageReplyBO msgReplyBO) {
        goodsDao.msg_reply(msgReplyBO);
    }

    /**
     * 前台根据keyword模糊查询商品信息，并返回
     * @param keyword
     * @return
     */
    public List<SearchGoodsVO> searchGoods(String keyword) {
        return goodsDao.searchGoods(keyword);
    }
}
